import {getThemeColor,getColor} from "./utils.js"
export default class StyleConvertor {
    // static getStyle(style,theme = null){
    //     const cellStyle = {
    //             ...this.getAlignmentStyle(style.alignment),
    //             ...this.getFont(style.font,theme),
    //             ...this.getFill(style.fill,theme),
    //         };
    //     return cellStyle;
    // }
    static getStyle(style,theme = null,align=true,font=true,fill=true){
        const cellStyle = {
                ...(align && this.getAlignmentStyle(style.alignment)),
                ...(font  && this.getFont(style.font,theme)),
                ...(fill && this.getFill(style.fill,theme)),
            };
        return cellStyle;
    }
    static getAlignmentStyle(alignment){
        let align = alignment || {};
        let horizontal = align.horizontal || 'flex-start';
        horizontal = horizontal == 'left' ? 'flex-start' : horizontal;
        horizontal = horizontal == 'right' ? 'flex-end' : horizontal;
        let textAlign = align.horizontal;
        let vertical = align.vertical || 'center';
        vertical = vertical == 'middle' ? 'center' : vertical;
        let wrapText = align.wrapText || false;
        let whiteSpace ='pre';
        if(wrapText){
            whiteSpace ='pre-wrap'
        }
        const alignStyle = {
            justifyContent: horizontal,
            alignItems: vertical,
            textAlign,
            whiteSpace
        };
        return alignStyle;
    }
    static getFont(font,theme = null){
        let fnt = font || {};
        const fontStyle = {
            fontFamily: fnt.name || 'Arial',
            fontSize: fnt.size ? fnt.size + 'pt' : '12pt',
            fontWeight: fnt.bold ? 'bold' : 'normal',
            fontStyle: fnt.italic ? 'italic' : 'normal',
            textDecoration: fnt.underline ? 'underline' : 'none',
            color: getColor(fnt.color,theme) || '#000000',
        };
        return fontStyle;
    }
    static getFill(fill,theme = null){
        if(!fill) return {};
        let bkg = getColor(fill.fgColor,theme) || 'none';
        const fillStyle = {
            backgroundColor: bkg,
        };
        return fillStyle;
    }
    static getBorderStyle(style){
        if(!style || !style.border) return {};
        return this.getBorder(style.border);
    }
    static getBorder(border){
        if (!border) return '';

        const out = {};
        const edgeMap = ['left', 'right', 'top', 'bottom'];

        // 线宽映射
        const widthMap = {
            thin: 1,
            medium: 2,
            thick: 3,
            dashed: 1,
            dotted: 1,
            double: 3,
            hair: 1,
            slantDashDot: 1,
            mediumDashed: 2,
            dashDot: 1,
            mediumDashDot: 2,
            dashDotDot: 1,
            mediumDashDotDot: 2,
        };

        // 线型映射
        const styleMap = {
            thin: 'solid',
            medium: 'solid',
            thick: 'solid',
            dashed: 'dashed',
            dotted: 'dotted',
            double: 'double',
            hair: 'solid',
            slantDashDot: 'dashed',
            mediumDashed: 'dashed',
            dashDot: 'dashed',
            mediumDashDot: 'dashed',
            dashDotDot: 'dashed',
            mediumDashDotDot: 'dashed',
        };

        edgeMap.forEach(edge => {
            const b = border[edge];
            if (!b || !b.style) return;          // 没有边框

            const w = widthMap[b.style] || 1;
            const s = styleMap[b.style] || 'solid';

            // 颜色：优先 argb，其次随便给一个默认黑
            let color = '#000';
            if (b.color && b.color.argb) {
            color = '#' + b.color.argb.slice(2, 8);   // ARGB -> #RRGGBB
            }
            out[`border-${edge}`] = `${w}px ${s} ${color}`;
        });
        return out;
    }
}